package org.jeecg.modules.demo.crm.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.dto.InvoiceDTO;
import org.jeecg.modules.demo.crm.entity.CCompanyInfoDetails;
import org.jeecg.modules.demo.crm.entity.CInvoiceSplit;
import org.jeecg.modules.demo.crm.entity.CPaymentRequest;
import org.jeecg.modules.demo.crm.entity.CPaymentRequestApply;
import org.jeecg.modules.demo.crm.mapper.CCompanyInfoDetailsMapper;
import org.jeecg.modules.demo.crm.mapper.CInvoiceSplitMapper;
import org.jeecg.modules.demo.crm.mapper.CPaymentRequestApplyMapper;
import org.jeecg.modules.demo.crm.mapper.CPaymentRequestMapper;
import org.jeecg.modules.demo.crm.service.ICPaymentRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 付款记录
 * @Author: jeecg-boot
 * @Date:   2025-06-20
 * @Version: V1.0
 */
@Service
public class CPaymentRequestServiceImpl extends ServiceImpl<CPaymentRequestMapper, CPaymentRequest> implements ICPaymentRequestService {

    @Autowired
    CPaymentRequestMapper cPaymentRequestMapper;
    @Autowired
    CPaymentRequestApplyMapper cPaymentRequestApplyMapper;

    @Autowired
    CInvoiceSplitMapper cInvoiceSplitMapper;

    @Autowired
    CCompanyInfoDetailsMapper cCompanyInfoDetailsMapper;

    @Override
    public void payment(CPaymentRequest cPaymentRequest) {

        cPaymentRequestMapper.insert(cPaymentRequest);
        //如果有申请打款记录id 去修改状态
        CPaymentRequestApply cPaymentRequestApply = cPaymentRequestApplyMapper.selectById(cPaymentRequest.getApplyId());
        if(cPaymentRequestApply != null){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            cPaymentRequestApply.setStatus("已付款");
            cPaymentRequestApply.setFinancialTime(new Date());
            cPaymentRequestApply.setFinancialUserId(sysUser.getId());
            cPaymentRequestApply.setRequestId(cPaymentRequest.getId());
            cPaymentRequestApplyMapper.updateById(cPaymentRequestApply);
        }
        List<InvoiceDTO> invoiceList = cPaymentRequest.getInvoiceList();
        for (InvoiceDTO invoiceDTO : invoiceList) {
            CInvoiceSplit split = CInvoiceSplit.builder()
                    .invoiceId(invoiceDTO.getId())
                    .requestId(cPaymentRequest.getId())
                    .amount(invoiceDTO.getAmount())
                    .splitAmount(invoiceDTO.getSplitAmount())
                    .projectId(cPaymentRequest.getProjectId())
                    .subProjectId(cPaymentRequest.getSubProjectId())
                    .contractorId(cPaymentRequest.getContractorId())
                    .build();
            cInvoiceSplitMapper.insert(split);
        }
        //本次打款金额(负数)

        BigDecimal paymentAmount = cPaymentRequest.getPaymentAmount();
        CCompanyInfoDetails companyInfoDetails = new CCompanyInfoDetails();
        companyInfoDetails.setCompanyId(cPaymentRequest.getCompanyId());
        companyInfoDetails.setProjectId(cPaymentRequest.getProjectId());
        companyInfoDetails.setSubProjectId(cPaymentRequest.getSubProjectId());
        companyInfoDetails.setRequestId(cPaymentRequest.getId());
        companyInfoDetails.setAmount(paymentAmount.negate());
        cCompanyInfoDetailsMapper.insert(companyInfoDetails);
    }

    @Override
    public void edit(CPaymentRequest cPaymentRequest) {
        List<InvoiceDTO> invoiceList = cPaymentRequest.getInvoiceList();
        //新增拆分数据
        // 清除记录
        cInvoiceSplitMapper.deleteSplit(cPaymentRequest.getId());
        //记录
        for (InvoiceDTO invoiceDTO : invoiceList) {
            CInvoiceSplit split = CInvoiceSplit.builder()
                    .invoiceId(invoiceDTO.getId())
                    .requestId(cPaymentRequest.getId())
                    .amount(invoiceDTO.getAmount())
                    .splitAmount(invoiceDTO.getSplitAmount())
                    .projectId(cPaymentRequest.getProjectId())
                    .subProjectId(cPaymentRequest.getSubProjectId())
                    .contractorId(cPaymentRequest.getContractorId())
                    .build();
            cInvoiceSplitMapper.insert(split);
        }
        cPaymentRequestMapper.updateById(cPaymentRequest);


        cCompanyInfoDetailsMapper.deleteByRequestId(cPaymentRequest.getId());
        //本次打款金额(负数)
        CCompanyInfoDetails byRequestId = cCompanyInfoDetailsMapper.findByRequestId(cPaymentRequest.getId());
        if(byRequestId == null){
            BigDecimal paymentAmount = cPaymentRequest.getPaymentAmount();
            CCompanyInfoDetails companyInfoDetails = new CCompanyInfoDetails();
            companyInfoDetails.setCompanyId(cPaymentRequest.getCompanyId());
            companyInfoDetails.setProjectId(cPaymentRequest.getProjectId());
            companyInfoDetails.setSubProjectId(cPaymentRequest.getSubProjectId());
            companyInfoDetails.setRequestId(cPaymentRequest.getId());
            companyInfoDetails.setAmount(paymentAmount.negate());
            cCompanyInfoDetailsMapper.insert(companyInfoDetails);
        }

    }
}
